package net.tv90.bfmccore.command;

import net.tv90.bfmccore.BFMCCore;
import net.tv90.bfmccore.battlepass.Battlepass;
import net.tv90.bfmccore.listener.AntiCheatListener;
import org.bitterorange.flansmodslain.FlansModSlain;
import org.bitterorange.flansmodslain.actions.Point;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ManageCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("manage")){
            if (args.length == 0) {
                sender.sendMessage("/manage vip <玩家名>");
                sender.sendMessage("/manage devip <玩家名>");
                sender.sendMessage("/manage moneyall <金额>");
                sender.sendMessage("/manage rank <玩家名> <金额>");
                sender.sendMessage("/manage bps <玩家名> <数量>");
                sender.sendMessage("/manage look <玩家1>");
                sender.sendMessage("/manage unlook");
                sender.sendMessage("/manage fixmode");
                return true;
            }
            if (args[0].equalsIgnoreCase("vip")) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + args[1] + " permission set bfmc.vip2 true");
            }
            if (args[0].equalsIgnoreCase("devip")) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "lp user " + args[1] + " permission unset bfmc.vip2");
            }
            if(args[0].equalsIgnoreCase("moneyall")){
                int money = Integer.parseInt(args[1]);
                Bukkit.getOnlinePlayers().forEach(player -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco give " + player.getName() + " " + money));
            }
            if(args[0].equalsIgnoreCase("clearmoney")){
                Bukkit.getOnlinePlayers().forEach(player -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco set " + player.getName() + " 0"));
            }
            if(args[0].equalsIgnoreCase("rank")){
                Player player = Bukkit.getPlayer(args[1]);
                for(int i = 0; i < Integer.parseInt(args[2]) / 100; i++){
                    Point.add(player, 100);
                }
            }
            if(args[0].equalsIgnoreCase("bps")){
                Player player = Bukkit.getPlayer(args[1]);
                Battlepass.addBattlepassPoint(player, Integer.parseInt(args[2]));
            }
            if(args[0].equalsIgnoreCase("look")){
                if(sender instanceof Player) {
                    AntiCheatListener.lookerList.add(sender.getName());
                    AntiCheatListener.antiCheat_pool.execute(() -> {
                        while (AntiCheatListener.lookerList.contains(sender.getName())) {
                            AntiCheatListener.syncPlayer2Player(Bukkit.getPlayer(args[1]), (Player) sender);
                        }
                    });
                }
            }
            if(args[0].equalsIgnoreCase("unlook")){
                if(sender instanceof Player) {
                    AntiCheatListener.lookerList.remove(sender.getName());
                }
            }
            if(args[0].equalsIgnoreCase("fixmode")){
                BFMCCore.fixMode = !BFMCCore.fixMode;
                System.out.println("FixMode: " + BFMCCore.fixMode);
            }
            if(args[0].equalsIgnoreCase("respawn")){
                Player player = Bukkit.getPlayer(args[1]);
                player.spigot().respawn();
            }
            return true;
        }
        return true;
    }
}
